package com.domel76;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.domel76.quickfilm.common.Constans;

import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListAdapter;
import android.widget.SimpleAdapter;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class Seans extends ListActivity {
	ArrayList<String> seanses = new ArrayList<String>();
	List<Map<String, String>> data = new ArrayList<Map<String, String>>();

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		String whereCinema = "";
		String whereFilm = "";

		Bundle extras = getIntent().getExtras();
		if (extras != null) {
			int cinemaId = extras.getInt("cinemaId", -1);
			if (cinemaId != -1) {
				whereCinema = " AND c.id = " + cinemaId + " ";
			}

			int filmId = extras.getInt("filmId", -1);
			if (filmId != -1) {
				whereFilm = " AND f.id = " + filmId + " ";
			}
		}

		SQLiteDatabase baza = null;

		try {

			baza = this.openOrCreateDatabase(Constans.DB_NAME, MODE_PRIVATE, null);
			Cursor cursor = baza
					.rawQuery(
							"SELECT 1 as level, s.hour as hour, f.title as title, c.name as name, f.id as filmId  FROM Film as f join Seans as s on (s.film_id=f.id) join Cinema as c on (c.id=s.cinema_id) WHERE hour >= strftime('%H:%M','now')"
									+ whereCinema
									+ whereFilm
									+ "UNION SELECT 2 as level, s.hour as hour, f.title as title, c.name as name, f.id as filmId  FROM Film as f join Seans as s on (s.film_id=f.id) join Cinema as c on (c.id=s.cinema_id) WHERE hour < strftime('%H:%M','now')"
									+ whereCinema + whereFilm + " ORDER by level, hour ", null);

			
			if (cursor.moveToFirst()) {
				do {
					String hour = cursor.getString(cursor.getColumnIndex("hour"));
					String name = cursor.getString(cursor.getColumnIndex("name"));
					String title = cursor.getString(cursor.getColumnIndex("title"));
					String filmIdField = cursor.getString(cursor.getColumnIndex("filmId"));

					Map<String, String> map = new HashMap<String, String>();
					map.put("hour", hour);
					map.put("name", name);
					map.put("title", title);
					map.put("filmId", filmIdField);

					data.add(map);
				} while (cursor.moveToNext());
			}

			// this.setListAdapter(new ArrayAdapter<String>(this,
			// android.R.layout.simple_list_item_1, seanses));

			ListAdapter a = new SimpleAdapter(this, data, R.layout.seans, new String[] { "hour", "name", "title" }, new int[] { R.id.hour, R.id.name,
					R.id.title });

			/*
			 * ListAdapter adapter = new SimpleCursorAdapter(this,
			 * R.layout.seans, cursor, new String[] { "hour", "name", "title" },
			 * new int[] { R.id.hour, R.id.name, R.id.title });
			 */
			this.setListAdapter(a);

		} catch (SQLiteException e) {
			Log.e(getClass().getSimpleName(), "Could not create or Open the database");
		} catch (Exception e) {

		} finally {
			if (baza != null)
				baza.close();
		}
		getListView().setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
				Intent intent = new Intent(getApplicationContext(), FilmDetails.class);
				intent.putExtra("filmId", Integer.valueOf(data.get(arg2).get("filmId")));
				startActivityForResult(intent, 1339);
			}
		});
	}

	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		/* Place out code to react on Activity-Result here. */
		super.onActivityResult(requestCode, resultCode, data);
		if (requestCode == 1340) {

			Toast tost = Toast.makeText(getApplicationContext(), data.getStringExtra("cinemaId"), 1000);
			tost.show();
		}
	}

}